package com.google.code.UtopiaXml;

import java.util.Objects;

/**
 * Class representing a single xs:attribute
 *
 * @author Michael Simacek
 */
public class Attribute implements Property {

    private String attrName;
    private Property prop;

    /**
     * Creates attribute with given type and name
     *
     * @param type type of attribute
     * @param name original name of attribute from {@literal @}name of
     * xs:attribute
     */
    public Attribute(SimpleType type, String name) {
        this.attrName = name;
        prop = new SingleProperty(type, attrName);
    }

    /**
     * Returns original unconverted name (for example "first-attribute").
     *
     * @return original name of the attribute
     */
    public String getAttributeName() {
        return attrName;
    }

    @Override
    public void generateDeclaration(CodeBuilder cb) {
        prop.generateDeclaration(cb);
    }

    @Override
    public void generateAccessors(CodeBuilder cb) {
        prop.generateAccessors(cb);
    }

    @Override
    public String getName() {
        return prop.getName();
    }

    @Override
    public SimpleType getType() {
        return (SimpleType) prop.getType();
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 89 * hash + Objects.hashCode(this.attrName);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Attribute other = (Attribute) obj;
        if (!Objects.equals(this.attrName, other.attrName)) {
            return false;
        }
        return true;
    }
}
